<link href="sales/styles.css" rel="stylesheet" type="text/css">
<script language="javascript" type="text/javascript">
     var imgDir = "images/";
     var preloadImages = new Array();
     preloadImages[0] = new Image();
     preloadImages[0].src = imgDir + "spacer.gif";
     preloadImages[1] = new Image();
     preloadImages[1].src = imgDir + "callout_a_1.gif";
     preloadImages[2] = new Image();
     preloadImages[2].src = imgDir + "callout_a_2.gif";
     preloadImages[3] = new Image();
     preloadImages[3].src = imgDir + "callout_a_3.gif";
     preloadImages[4] = new Image();
     preloadImages[4].src = imgDir + "callout_a_4.gif";
</script>
<script language="JavaScript" type="text/javascript" src="overlib.js"></script>
<?
include('issued_dates.php');
function comments_table($msg_arr,$unique){
	$res = '';
	foreach($msg_arr as $k=>$v){
		$res .= "<tr><td>$v[date]</td><td>$v[msg]</td></tr>";
	}
	if($res){
		$res = "<a href=\"javascript:void(0);\" onmouseover=\"return overlib('<table width=300 align=center>$res</table>', VAUTO, HAUTO, STICKY, MOUSEOFF);\" onmouseout=\"return nd(500);\">[comments]</a>";
	}else{
		$res='-';
	}
	return $res;
}

function for_issued($period,$data){
	$arr = $GLOBALS[issued_dates_array];
//	$arr = array('2005-09'=>'2005-11-04','2005-10'=>'2005-12-06','2005-11'=>'2006-01-06','2005-12'=>'2006-02-03');
        $total = sprintf('%0.2f',0);
        $res ='';

        if(date('j')<7){
                $current_period = date('Y-m',mktime(0,0,0,date('m')-1,1,date('Y')));
                $previous_period = date('Y-m',mktime(0,0,0,date('m')-2,1,date('Y')));
        }else{
                $current_period = date('Y-m');
                $previous_period = date('Y-m',mktime(0,0,0,date('m')-1,1,date('Y')));
        }

        foreach($data as $per => $line){
                if(strtotime($line['period'].'-01') < strtotime($current_period.'-01')){
                        $total = bcadd($total, $line['amount'], 2);
                }
        }

        if($total >= $data[$period]['release_level'] and strtotime($period.'-01') < strtotime($current_period.'-01')){
                $res = '['.date('m/d/Y',strtotime($arr[$previous_period])).']';
        }elseif($period==$current_period && $data[$period]['amount'] >= $data[$period]['release_level']){
        	$res = '['.date('m/d/Y',strtotime($arr[$current_period])).']';
        }
        return $res;
}

function return_pm_tooltip($data,$type=''){
        switch($data[payment_method]){
                case 'check' :
			$pm_tooltip = "<table align=center width=250 border=0 cellspacing=0 cellpadding=0><tr>".($type=='upcoming' ? "<td align=center><b>Release Level</b></td>" : "")."<td align=center><b>Recipient\'s Address</b></td></tr><tr>".($type=='upcoming' ? "<td align=center>\$$data[release_level]</td>" : "")."<td align=center>".ucwords("$data[first_name] $data[last_name]")."<br>".($data[address2] ? ucwords("$data[address1], $data[address2]") : ucwords($data[address1]))."<br>".(ucwords($data[city]).', '.strtoupper("$data[state] $data[zip]"))."<br>$data[country_long]</td></tr></table>";
                break;
                case 'paypal' :
			$pm_tooltip = "<table align=center width=220 border=0 cellspacing=0 cellpadding=0><tr>".($type=='upcoming' ? "<td align=center><b>Release Level</b></td>" : "")."<td align=center><b>PayPal E-mail</b></td></tr><tr>".($type=='upcoming' ? "<td align=center>\$$data[release_level]</td>" : "")."<td align=center>$data[paypal_email]</td></tr></table>";
                break;
                case 'wallet' :
                        if($type=='upcoming'){
				$pm_tooltip = "<table align=center width=130 border=0 cellspacing=0 cellpadding=0><tr><td align=center><b>Release Level</b></td></tr><tr><td align=center>\$$data[release_level]</td></tr></table>";
                        }else{
				$pm_tooltip = "<table align=center width=130 border=0 cellspacing=0 cellpadding=0><tr><td align=center>Wallet [#$data[payment_id]]</td></tr></table>";
                        }
                break;
                case 'wire' :
                        $pm_tooltip = "<table align=center width=360 border=0 cellspacing=0 cellpadding=0><tr>".($type=='upcoming' ? "<td align=center><b>Release Level</b></td>" : "")."<td align=center><b>Bank Info</b></td></tr><tr>".($type=='upcoming' ? "<td align=center>\$$data[release_level]</td>" : "")."<td><b>Swift Code : </b>$data[swift_code]<br><b>Bank Name : </b>$data[bank_name]<br><b>Bank Address : </b>$data[bank_address]<br><b>Account Number : </b>$data[account_number]<br><b>Routing Number : </b>$data[routing_number]<br><b>Account Holder : </b>$data[account_holder]</td></tr></table>";
                break;
        }
        return $pm_tooltip;
}
//if($_SERVER['REMOTE_ADDR']=='83.228.120.103'){
//	print_r(for_issued('2005-10',get_reseller_commission_($id)));
//}
$map = array(
	'payment_method' => array('wire'=>'Bank Wire','paypal'=>'PayPal','wallet'=>'Wallet','check'=>'Check')
);
$result_table = "<table class='bTable' width='100%' cellpadding=0 cellspacing=0>
		<tr>
			<td colspan='5'>New Payment History</td>
			<td align=right><a href=\"javascript: void (window.open('sales/payment_history_help.html', '', 'width=800,height=600,scrollbars=yes'));\">Help</a></td>
		</tr>
		<tr>
			<th>Period</th>
			<th>Sales</th>
			<th>Commission</th>
			<th>Payment Method</th>
			<th>Payment Status</th>
			<th>Comments</th>
		<tr>";

$payment_history = get_payment_history__($id);
$upcoming_periods = get_reseller_commission_($id);

$params1[sid] = $sid;
$params1[action] = "payment_sales";

foreach($upcoming_periods as $period => $line){

	$params1['date'] = '';
	$params1['period'] = $period;
	$url1 = "$baseURL/$webDIR/?".encode_params($params1);

	$result_table .= "<tr>
		<td><a href='$url1' target=_blank>".date('F Y',strtotime($period.'-01'))."</a></td>
		<td align=center>
			<a href='$url1' target=_blank onmouseover=\"return overlib('<table width=130 align=center><tr><td align=right><b>Your Sales: </b></td><td>$line[count_sales]</td></tr><tr><td align=right><b>Affiliate Sales: </b></td><td>$line[count_affiliates]</td></tr></table>', VAUTO, HAUTO,STICKY, MOUSEOFF);\" onmouseout=\"return nd(500);\">$line[count]</a>			
		</td>
		<td align=right>
			<a href='$url1' target=_blank onmouseover=\"return overlib('<table width=170 align=center><tr><td nowrap align=right><b>Your Sales<br>Amount to: </b></td><td align=right>\$".number_format($line[amount_sales], 2, '.', ',')."</td></tr><tr><td nowrap align=right><b>Affiliate Sales<br>Amount to: </b></td><td align=right>\$".number_format($line[amount_affiliates], 2, '.', ',')."</td></tr><tr><td nowrap align=right><b>Total<br>Commission:  </b></td><td align=right>\$".number_format($line[amount],2,'.',',')."</td></tr></table>', VAUTO, HAUTO ,STICKY, MOUSEOFF);\" onmouseout=\"return nd(500);\">\$".number_format($line[amount],2,'.',',')."</a>
		</td>
		<td align=center>
			<a href='$url1' target=_blank onmouseover=\"return overlib('".return_pm_tooltip($line,'upcoming')."', VAUTO, HAUTO,STICKY, MOUSEOFF);\" onmouseout=\"return nd(500);\">".$map['payment_method'][($line['payment_method'])]."</a>
		</td>
		<td align=right>upcoming ".for_issued($period,$upcoming_periods)."</td>
		<td align=center>-</td>
	<tr>";
}

foreach($payment_history as $k=>$payment){
	$params1['date'] = $payment[issued];
	$url1 = "$baseURL/$webDIR/?".encode_params($params1);
	if($payment[payment_method]=='paypal'){
		$tmp1 = $map['payment_method'][($payment[payment_method])].($payment[paypal_transaction_id] ? ' [ID '.$payment[paypal_transaction_id].']' : '');
                if($payment[status]=='none'){
                        $tmp2 = 'upcoming ['.date('m/d/Y',strtotime($payment['change_status_date'])).']';
                }else{
                        $tmp2 = $payment['status'].' ['.date('m/d/Y',strtotime($payment['change_status_date'])).']';
                }
	}elseif($payment[payment_method]=='check'){
		$tmp1 = $map['payment_method'][($payment[payment_method])].' [#'.$payment[check_number].']';
		if($payment[status]=='none'){
			$tmp2 = 'sent ['.date('m/d/Y',strtotime($payment['change_status_date'])).']';
		}else{
			$tmp2 = $payment['status'].' ['.date('m/d/Y',strtotime($payment['change_status_date'])).']';
		}
	}else{
		$tmp1 = $map['payment_method'][($payment[payment_method])].' [#'.$payment[payment_id].']';
                if($payment[status]=='none'){
                        $tmp2 = 'upcoming ['.date('m/d/Y',strtotime($payment['change_status_date'])).']';
                }else{
                        $tmp2 = $payment['status'].' ['.date('m/d/Y',strtotime($payment['change_status_date'])).']';
                }
	}

	$result_table .= "<tr>
		<td><a href='$url1' target=_blank>$payment[memo]</a></td>
		<td align=center>
			<a href='$url1' target=_blank onmouseover=\"return overlib('<table width=130 align=center><tr><td align=right><b>Your Sales: </b></td><td>$payment[count_sales]</td></tr><tr><td align=right><b>Affiliate Sales: </b></td><td>$payment[count_affiliates]</td></tr></table>', VAUTO, HAUTO,STICKY, MOUSEOFF);\" onmouseout=\"return nd(500);\">$payment[count]</a>
		</td>
		<td align=right>
			<a href='$url1' target=_blank onmouseover=\"return overlib('<table width=170 align=center><tr><td nowrap align=right><b>Your Sales<br>Amount to: </b></td><td align=right>\$".number_format($payment[amount_sales],2,'.',',')."</td></tr><tr><td nowrap align=right><b>Affiliate Sales<br>Amount to: </b></td><td align=right>\$".number_format($payment[amount_affiliates],2,'.',',')."</td></tr><tr><td nowrap align=right><b>Total<br>Commission: </b></td><td align=right>\$".number_format($payment[amount],2,'.',',')."</td></tr></table>', VAUTO, HAUTO,STICKY, MOUSEOFF);\" onmouseout=\"return nd(500);\">\$".number_format($payment[amount],2,'.',',')."</a>
		</td>
		<td align=center>
			<a href='$url1' target=_blank onmouseover=\"return overlib('".return_pm_tooltip($payment)."', VAUTO, HAUTO,STICKY, MOUSEOFF);\" onmouseout=\"return nd(500);\">$tmp1</a>
		</td>
		<td align=right>$tmp2</td>
		<td align=center>".(comments_table($payment[messages],$k))."</td>
	<tr>";
}

if(!$upcoming_periods and !$payment_history){
	$result_table .= "
				<tr>
					<td align=center colspan=6>No Payment History Avaiable</td>
				</tr>
			";
}
$result_table .= '</table>';
echo $result_table;
?>